/**
 * @{#} ${ classPre }Controller.java Created on ${ date }
 *
 * Copyright (c) ${ year } by hiwitech.com software.
 */
package ${packageName}.controller.${moduleName};

<#--FIXME-->
import ${packageName}.common.restapi.ResultCodeEnum;
import ${packageName}.common.restapi.ResponseObject;
import ${packageName}.common.util.DateUtils;
import ${packageName}.common.util.PoiExcelUtils;
import ${packageName}.common.error.BizException;

import ${packageName}.controller.BaseController;
import ${packageName}.model.page.PageInfo;
import ${packageName}.model.page.PageRequest;
import ${packageName}.model.request.${moduleName}.${ classPre }Request;
import ${packageName}.model.request.query.${ classPre }Query;
import ${packageName}.model.vo.${ classPre }VO;
import ${packageName}.service.${moduleName}.${ classPre }Service;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.List;
/**
 * @author hiwitech
 * @version 1.0
 */
@Controller
@RequestMapping("/${moduleName}/${ firstLowerClassPre }")
public class ${ classPre }Controller extends BaseController {

	@Autowired
	private ${ classPre }Service ${ firstLowerClassPre }Service;

	/**
	 * 功能的首页
	 */
	@RequestMapping(value = "/index")
	public String index(Model model) {
		return "${moduleName}/${ firstLowerClassPre }";
	}

	/**
	 * 保存 ${ classPre }信息
	 *
	 * @param ${ firstLowerClassPre }Request
	 */
	@ResponseBody
	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	public ResponseObject<Object> save(${ classPre }Request ${ firstLowerClassPre }Request) {
		// do something
		
    	return new ResponseObject<Object>(${ firstLowerClassPre }Service.saveOrUpdate(${ firstLowerClassPre }Request));
    }

	/**
	 * 删除 ${ classPre }信息
	 * 
	 * @param id
	 */
	@ResponseBody
	@RequestMapping(value = "/delete.do", method = RequestMethod.POST)
	public ResponseObject<Object> delete(Long id) {
		return new ResponseObject<Object>(${ firstLowerClassPre }Service.deleteById(id));
	}

	/**
	 * 分页查询${ classPre }VO信息
	 * 
	 * @param pageRequest
	 * @param query
	 */
    @ResponseBody
    @RequestMapping(value = "/pageList.do")
	public PageInfo<${ classPre }VO> pageList(PageRequest pageRequest, ${ classPre }Query query) {
		return new PageInfo<${ classPre }VO>(${ firstLowerClassPre }Service.list(pageRequest, query));
	}

	/**
	 * 根据ID查找${ classPre }VO信息
	 *
	 * @param id
	 */
	@ResponseBody
	@RequestMapping(value = "/getById.do")
	public ResponseObject<Object> getById(Long id) {
		return new ResponseObject<Object>(${ firstLowerClassPre }Service.loadVOById(id));
	}

	<#if tableConfig.needExport>
	/**
	 * 导出数据
	 *
	 * @param query
	 */
	@ResponseBody
	@RequestMapping(value = "/exportList.do")
	public void exportList(${ classPre }Query query, HttpServletResponse response) throws Exception {
		// 验证数据数量
		PageInfo pageInfo = new PageInfo<${ classPre }VO>(${ firstLowerClassPre }Service.list(new PageRequest(0, 1), query));
		if (pageInfo.getCount() > 10000) {
			throw new BizException(ResultCodeEnum.ERROR_NOT_ALLOW, "单次最多导出1W条");
		}

		// 查询需要导出的数据
		List<${ classPre }VO> lists = ${ firstLowerClassPre }Service.list(query);

		//设置需要导出的数据
		String[] headers = new String[]{<#list tableConfig.exportFields as field>"${fieldMap[field].viewName}@${fieldMap[field].proName}"<#if field_has_next>, </#if></#list>};

		// 生成excel文件对象
		HSSFWorkbook hssfWorkbook = PoiExcelUtils.createExcel2Export("导出${tableConfig.viewTitle}", "${tableConfig.viewTitle}", headers, lists);

		response.reset();
		response.setContentType("application/octet-stream;charset=UTF-8");
		// 设置文件名等信息
		response.setHeader("Content-Disposition", "attachment;filename=${ firstLowerClassPre }_" + DateUtils.getToday() + ".xls");

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		hssfWorkbook.write(os);

		OutputStream stream = response.getOutputStream();
		stream.write(os.toByteArray());
		stream.flush();
		stream.close();
		hssfWorkbook.close();
	}
	</#if>
}